home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / IFF / saveilbm.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  6KB  |  209 lines

  1.  
  2. /***************************************************************************
  3. *  SaveILBM.c --  Save raw raster image as ILBM file
  4. *                 by Carolyn Scheppner  CBM  01/86
  5. *     Using IFF rtns by J.Morrison and S.Shaw of Electronic Arts
  6. *
  7. * Linkage information:
  8. * FROM     LStartup.obj, SaveILBM.o, iffw.o, ilbmw.o, packer.o
  9. * TO       SaveILBM
  10. * LIBRARY  LC.lib, Amiga.lib
  11. ***************************************************************************/
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <libraries/dos.h>
  16. #include <graphics/gfxbase.h>
  17. #include <graphics/rastport.h>
  18. #include <graphics/gfx.h>
  19. #include <graphics/view.h>
  20.  
  21. #include <intuition/intuition.h>
  22. #include <intuition/intuitionbase.h>
  23.  
  24. #include <libraries/dos.h>
  25. #include <iff/ilbm.h>
  26.  
  27. #define bufSize 512
  28.  
  29. struct IntuitionBase *IntuitionBase;
  30. struct GfxBase       *GfxBase;
  31. ULONG  DosBase;
  32.  
  33. struct Window   *activeWindow;
  34. struct Screen   *activeScreen;
  35. struct Screen   *firstScreen;
  36.  
  37. struct Screen   *nextScreen, *highScreen;
  38. struct View     *thisView;
  39. struct View     *picView = 0;
  40. struct ViewPort *picViewPort;
  41. struct BitMap   *picBitMap;
  42. WORD            *picColorTable;
  43.  
  44.  
  45. /* main() */
  46. void main(argc,argv)
  47. int  argc;
  48. char **argv;
  49.    {
  50.    LONG file;
  51.    IFFP iffp = NO_FILE;
  52.    char ch[2];
  53.    BPTR CliInput;
  54.    SHORT topEdge;
  55.    int  i;
  56.  
  57.    if (argc < 2)
  58.       printf("Usage: 'SaveILBM filename'\n\n");
  59.    else
  60.       {
  61.       if ((IntuitionBase =
  62.          (struct IntuitionBase *)OpenLibrary("intuition.library",0))==NULL)
  63.       cleanexit(-1);
  64.  
  65.       if ((GfxBase =
  66.          (struct GfxBase *)OpenLibrary("graphics.library",0))==NULL)
  67.       cleanexit(-1);
  68.  
  69.       if ((DosBase = OpenLibrary("dos.library",0))==NULL )
  70.          cleanexit(-1);
  71.  
  72.       printf("\nMove screens so screen to save is highest.\n");
  73.       printf("Then click here and respond to prompt:\n\n");
  74.       printf("  Save highest screen <y or n> ? ");
  75.       CliInput = Input();
  76.       Read(CliInput,&ch[0],2);
  77.  
  78.       if((ch[0]=='y') || (ch[0]=='Y'))
  79.          {
  80.  
  81.          file = Open(argv[1], MODE_NEWFILE);
  82.      
  83.          if(file)
  84.             {
  85.             Write(file,"x",1);  /* so Seek to beginning works ? */
  86.  
  87.             Forbid();
  88.  
  89.             firstScreen  = IntuitionBase->FirstScreen;
  90.             topEdge = 400;
  91.             nextScreen = highScreen = firstScreen;
  92.             while( nextScreen != NULL )
  93.                {
  94.                if(nextScreen->TopEdge < topEdge)
  95.                   {
  96.                   topEdge = nextScreen->TopEdge;
  97.                   highScreen = nextScreen;
  98.                   }
  99.                nextScreen = nextScreen->NextScreen;
  100.                }
  101.  
  102.             Permit();
  103.  
  104.             picViewPort = &( highScreen->ViewPort );
  105.  
  106.             printf("\nWill save selected screen in 5 seconds.\n");
  107.             for(i=0;i<5*60;i++) WaitTOF();
  108.             printf("\nSaving highest screen as '%s'...\n", argv[1]);
  109.  
  110.             picBitMap = (struct BitMap*)picViewPort->RasInfo->BitMap;
  111.             picColorTable = (WORD *)picViewPort->ColorMap->ColorTable;
  112.  
  113.             iffp = PutPicture(file, picBitMap, picColorTable);
  114.  
  115.             Close(file);
  116.             }
  117.          }
  118.  
  119.       cleanup();
  120.       }
  121.    }
  122.  
  123.  
  124. cleanexit(error)
  125. int error;
  126.    {
  127.    cleanup();
  128.    exit(error);
  129.    }
  130.  
  131. cleanup()
  132.    {
  133.    if (DosBase) CloseLibrary(DosBase);
  134.    if (GfxBase) CloseLibrary(GfxBase);
  135.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  136.    }
  137.  
  138.  
  139. /** PutPicture() ***********************************************************
  140.  *
  141.  * Put a picture into an IFF file.
  142.  * This procedure calls PutAnILBM, passing in an <x, y> location of <0, 0>,
  143.  * a NULL mask, and a locally-allocated buffer. It also assumes you want to
  144.  * write out all the bitplanes in the BitMap.
  145.  *
  146.  ***************************************************************************/
  147. Point2D nullPoint = {0, 0};
  148.  
  149. IFFP PutPicture(file, bitmap, colorMap)
  150.       LONG file;  struct BitMap *bitmap;  WORD *colorMap;
  151.    {
  152.    BYTE buffer[bufSize];
  153.    return( PutAnILBM(file, bitmap, NULL,
  154.            colorMap, bitmap->Depth, &nullPoint,
  155.            buffer, bufSize) );
  156.    }    
  157.  
  158.    
  159. /** PutAnILBM() ************************************************************
  160.  *
  161.  * Write an entire BitMap as a FORM ILBM in an IFF file.
  162.  * This version works for any display mode (C. Scheppner).
  163.  *
  164.  * Normal return result is IFF_OKAY.
  165.  *
  166.  * The utility program IFFCheck would print the following outline of the
  167.  * resulting file:
  168.  *
  169.  *   FORM ILBM
  170.  *     BMHD
  171.  *     CMAP
  172.  *     BODY       (compressed)
  173.  *
  174.  ***************************************************************************/
  175. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  176.  
  177. IFFP PutAnILBM(file, bitmap, mask, colorMap, depth, xy, buffer, bufsize)
  178.       LONG file;  struct BitMap *bitmap;  BYTE *mask;  WORD *colorMap;
  179.       UBYTE depth;  Point2D *xy;
  180.       BYTE *buffer;  LONG bufsize;
  181.    {
  182.    BitMapHeader bmHdr;
  183.    GroupContext fileContext, formContext;
  184.    IFFP ifferr;
  185.    WORD pageWidth, pageHeight;
  186.  
  187.    pageWidth  = (bitmap->BytesPerRow) << 3;
  188.    pageHeight = bitmap->Rows;
  189.  
  190.    ifferr = InitBMHdr(&bmHdr, bitmap, mskNone,
  191.                       cmpByteRun1, 0, pageWidth, pageHeight);
  192.    /* You could write an uncompressed image by passing cmpNone instead
  193.     * of cmpByteRun1 to InitBMHdr. */
  194.    bmHdr.nPlanes = depth;   /* This must be  <= bitmap->Depth */
  195.    if (mask != NULL) bmHdr.masking = mskHasMask;
  196.    bmHdr.x = xy->x;   bmHdr.y = xy->y;
  197.  
  198.    CkErr( OpenWIFF(file, &fileContext, szNotYetKnown) );
  199.    CkErr(StartWGroup(&fileContext, FORM, szNotYetKnown, ID_ILBM, &formContext));
  200.  
  201.    CkErr( PutBMHD(&formContext, &bmHdr) );
  202.    CkErr( PutCMAP(&formContext, colorMap, depth) );
  203.    CkErr( PutBODY(&formContext, bitmap, mask, &bmHdr, buffer, bufsize) );
  204.  
  205.    CkErr( EndWGroup(&formContext) );
  206.    CkErr( CloseWGroup(&fileContext) );
  207.    return( ifferr );
  208.    }    
  209.